package com.yunhe.sms.controller;

import com.yunhe.commens.util.BASE64Utils;
import com.yunhe.sms.dto.Menus;
import com.yunhe.sms.entity.system.Premission;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.Loginservice;
import com.yunhe.sms.service.system.PremissionService;
import com.yunhe.sms.service.system.RoleService;
import com.yunhe.sms.service.system.impl.PremissionServiceImpl;
import com.yunhe.sms.service.system.impl.RoleServiceImpl;
import com.yunhe.webmvc.annocation.RequestBean;
import com.yunhe.webmvc.annocation.RequestMapping;
import com.yunhe.webmvc.annocation.ResponseJSON;
import com.yunhe.webmvc.mode.JsonResponce;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_DATE;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/26 0026 18:24
 * @Description: 登录控制
 */
@RequestMapping("/")
public class LoginController {
      private Loginservice loginservice= new Loginservice ();


    @RequestMapping
    public  String login(HttpServletRequest req){
        return "login";
    }

    /**
     * 从异步获取登录名和密码和验证码
     * @param user
     * @param yzm 验证码
     * @param resp
     * @param req
     * @return
     */
    @RequestMapping("login")
    @ResponseJSON
    public  JsonResponce index(@RequestBean User user,String yzm, HttpServletResponse resp , HttpServletRequest req)  {
        resp.setCharacterEncoding ( "utf-8" );
        User login = loginservice.login ( user );//
        HttpSession session = req.getSession ();
        String yzm1 = (String)session .getAttribute ( KAPTCHA_SESSION_KEY );
        Date date = (Date)session.getAttribute ( KAPTCHA_SESSION_DATE );
        int code= JsonResponce.CODE_SUCCESS;
        String msg="登陆成功！";
        if (null == yzm || yzm.isEmpty () || !yzm.equals ( yzm1 )|| (System.currentTimeMillis()-date.getTime ())/1000/60>15){
             code=JsonResponce.CODE_FAIL;
             msg="验证码错误或失效";
        } else  {
            if(null == login){
                code=JsonResponce.CODE_FAIL;
                msg="用户名或密码错误";
            }else{//登陆成功
                List<Menus> menusList = loginservice.premirole ( login.getRoleId () );
                session.setAttribute ( "menulist", menusList);//左侧菜单树存入request中时必须转发才能拿到值
                Map<String, Premission> allpremiss = loginservice.findAllpremiss ( login.getRoleId () );//获取用户的所有权限，放入map直接map中
                session.setAttribute ( "allpremi",allpremiss );
                session.setAttribute ( "userName", user.getUserName () );
                String userName = BASE64Utils.encrypt ( user.getUserName () ); //加密
                String password = BASE64Utils.encrypt ( user.getPassword () );//加密

                Cookie cookie = new Cookie ( "userName", userName);
                Cookie cookie1 = new Cookie ( "password", password);
                cookie.setPath ( "/" );
                cookie1.setPath ( "/" );
                cookie.setMaxAge ( 7 * 24 * 3600 );
                cookie1.setMaxAge ( 7 * 24 * 3600 );
                resp.addCookie ( cookie );
                resp.addCookie ( cookie1 );
            }
        }
        req.getSession ().removeAttribute ( KAPTCHA_SESSION_KEY );//验证码失效
        req.getSession ().removeAttribute ( KAPTCHA_SESSION_DATE );
        return new JsonResponce ( code,msg );
    }

    @RequestMapping("exit")
    public String exit(HttpServletRequest req){
        req.getSession ().removeAttribute ( "userName" );
        return "redirect:/";
    }
}
